package com.belly.model;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.belly.model.enums.FileGenerateTypeEnum;
import com.belly.model.enums.FileTypeEnum;
import com.belly.model.enums.ModelTypeEnum;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Belly
 * @version 1.1.0
 *
 * 元数据校验
 */
public class MetaValidator {

    public static void validAndFill(Meta meta){
        validAndFillMetaRoot(meta);
        validAndFillFileConfig(meta);
        validAndFillModeConfig(meta);
    }

    public static void validAndFillModeConfig(Meta meta){
        Meta.ModelConfig modelConfig = meta.getModelConfig();
        if (null == modelConfig){
            return;
        }

        //设置默认值
        List<Meta.ModelConfig.ModelInfo> modelInfoList = modelConfig.getModels();
        if (!CollectionUtil.isNotEmpty(modelInfoList)) {
            return;
        }
        for (Meta.ModelConfig.ModelInfo modelInfo : modelInfoList) {
            //不校验group
            String groupKey = modelInfo.getGroupKey();
            if (StrUtil.isNotEmpty(groupKey)){
                //private String allArgsStr = "\"--author\"," + "\"--outputText\"";
                //--author,--outputText
//                List<Meta.ModelConfig.ModelInfo> infos = modelInfo.getModels();
                String allArgsStr = modelInfo.getModels().stream()
                        .map(subModelInfo -> String.format("\"--%s\"", subModelInfo.getFieldName()))
                        .collect(Collectors.joining(","));
                modelInfo.setAllArgsStr(allArgsStr);
                continue;
            }

            //设置输出路径默认值
            String fieldName = modelInfo.getFieldName();
            if (StrUtil.isBlank(fieldName)){
                throw  new MetaException("未填写fieldName");
            }
            //设置默认类型值
            String modelInfoType = modelInfo.getType();
            if (StrUtil.isEmpty(modelInfoType)){
                modelInfo.setType(ModelTypeEnum.STRING.getValue());
            }
        }
    }

    public static void validAndFillFileConfig(Meta meta){
        //设置fileConfig默认值
        Meta.FileConfig fileConfig = meta.getFileConfig();
        if (null == fileConfig){
            return;
        }

        String sourceRootPath = fileConfig.getSourceRootPath();
        if (StrUtil.isBlank(sourceRootPath)){
            throw new MetaException("未填写sourceRootPath路径");
        }
        // todo
        String inputRootPath = fileConfig.getInputRootPath();
        String delInputRootPath =
//                ".source" + File.separator +
                FileUtil.getLastPathEle(Paths.get(sourceRootPath))
                        .getFileName().toString();
        if (StrUtil.isEmpty(inputRootPath)){
            fileConfig.setInputRootPath(delInputRootPath);
        }

        String outputRootPath = fileConfig.getOutputRootPath();
        String delOutputRootPath = "generated";
        if (StrUtil.isEmpty(outputRootPath)){
            fileConfig.setOutputRootPath(delOutputRootPath);
        }

        String fileConfigType = fileConfig.getType();
        String delValue = FileTypeEnum.DIR.getValue();
        if (StrUtil.isEmpty(fileConfigType)){
            fileConfig.setType(delValue);
        }

        //设置FileInfo默认值
        List<Meta.FileConfig.FileInfo> fileInfoList = fileConfig.getFiles();
        if (!CollectionUtil.isNotEmpty(fileInfoList)){
            return;
        }
        for (Meta.FileConfig.FileInfo fileInfo : fileInfoList) {
            String type = fileInfo.getType();
            //group不校验
            if (FileTypeEnum.GROUP.getValue().equals(type)){
                continue;
            }
            String inputPath = fileInfo.getInputPath();
            if (StrUtil.isBlank(inputPath)){
                throw new MetaException("未填写inputPath路径");
            }

            String outputPath = fileInfo.getOutputPath();
            if (StrUtil.isEmpty(outputPath)){
                fileInfo.setOutputPath(outputPath);
            }

            //有后缀为file，无则dir
            if (StrUtil.isBlank(type)){
                fileInfo.setType(FileTypeEnum.DIR.getValue());
            } else {
                fileInfo.setType(FileTypeEnum.FILE.getValue());
            }

            //文件以.ftl结尾为dynamic，其他则为static
            String generateType = fileInfo.getGenerateType();
            if (StrUtil.isBlank(generateType)){
                if (inputPath.endsWith(".ftl")){
                    fileInfo.setGenerateType(FileGenerateTypeEnum.DYNAMIC.getValue());
                } else {
                    fileInfo.setGenerateType(FileGenerateTypeEnum.STATIC.getValue());
                }
            }
        }
    }

    public static void validAndFillMetaRoot(Meta meta){
        //默认值设置
        meta.setName(StrUtil.blankToDefault(meta.getName(), "acm-template-pro"));
        meta.setDescription(StrUtil.emptyToDefault(meta.getDescription(), "belly模板生成器"));
        meta.setAuthor(StrUtil.emptyToDefault(meta.getAuthor(), "belly"));
        meta.setBasePackage(StrUtil.blankToDefault(meta.getBasePackage(), "com.belly.template"));
        meta.setVersion(StrUtil.emptyToDefault(meta.getVersion(), "1.1.0"));
        meta.setCreateTime(StrUtil.emptyToDefault(meta.getCreateTime(), DateUtil.now()));
    }
}
